package RentCarProject;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class ManagerLoginUI extends JFrame {
    String managername;
    String managerpassword;
    JFrame frame;
    JTextField usertext;
    JPasswordField passtext;
    MyButton loginbutton,returnbutton;
    JLabel bglabel,userlabel,passwordlabel;
    public ManagerLoginUI() {
        frame=this;
        setTitle("管理员登录");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(750,450);
        setLocationRelativeTo(null);
        setLayout(null);

        userlabel=new JLabel("用户名：");
        userlabel.setForeground(Color.white);
        userlabel.setFont(new Font("宋体",Font.BOLD,15));
        userlabel.setBounds(250,110,70,20);
        passwordlabel=new JLabel("密码：");
        passwordlabel.setForeground(Color.white);
        passwordlabel.setFont(new Font("宋体",Font.BOLD,15));
        passwordlabel.setBounds(250,160,70,20);

        usertext=new JTextField(20);
        usertext.setBounds(330,110,180,20);
        passtext=new JPasswordField(20);
        passtext.setBounds(330,160,180,20);
        loginbutton = new MyButton("登录");
        loginbutton.setBounds(250,250,100,40);
        returnbutton=new MyButton("返回用户登录");
        returnbutton.setBounds(400,250,150,40);
        bglabel = new JLabel(new ImageIcon("src/picture/login.png"));
        bglabel.setBounds(0,0,750,450);
        bglabel.setOpaque(false);

        loginbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                managername=usertext.getText();
                managerpassword=passtext.getText();
                try {
                    if(loGin()){
                        dispose();
                        new ManagerUI().init();
                    }else{
                        JOptionPane.showMessageDialog(frame,"登录失败！");
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        returnbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new LoginUI();
            }
        });

        add(userlabel);
        add(usertext);
        add(passwordlabel);
        add(passtext);
        add(loginbutton);
        add(returnbutton);
        add(bglabel);

        setVisible(true);
    }

    public boolean loGin() throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from manager where managername=? and managerpin=? ";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,managername);
        preparedStatement.setString(2,managerpassword);
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return true;
        }else{
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return false;
        }
    }

}
